package com.clothe.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import com.clothe.dao.IAdminInfoDAO;
import com.clothe.dao.impl.AdminInfoDAOImpl;
import com.clothe.database.DataBase;
import com.clothe.dto.AdminInfo;

public class AdminInfoService {
	IAdminInfoDAO adminInfoDAOImpl = new AdminInfoDAOImpl();

	public boolean save(AdminInfo adminInfo) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			adminInfoDAOImpl.setConn(connection);
			flag = adminInfoDAOImpl.save(adminInfo);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return flag;
	}

	public boolean delete(int id) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			adminInfoDAOImpl.setConn(connection);
			flag = adminInfoDAOImpl.delete(id);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return flag;
	}

	public boolean update(AdminInfo adminInfo) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			adminInfoDAOImpl.setConn(connection);
			flag = adminInfoDAOImpl.update(adminInfo);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return flag;
	}

	public AdminInfo get(int id) throws Exception {
		Connection connection = null;
		AdminInfo adminInfo = null;
		try {
			adminInfo = new AdminInfo();
			connection = DataBase.getConn();
			adminInfoDAOImpl.setConn(connection);
			adminInfo = adminInfoDAOImpl.get(id);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return adminInfo;

	}

	public List<AdminInfo> list() throws Exception {
		Connection connection = null;
		List<AdminInfo> adminInfoList = null;
		try {
			adminInfoList = new ArrayList<AdminInfo>();
			connection = DataBase.getConn();
			adminInfoDAOImpl.setConn(connection);
			adminInfoList = adminInfoDAOImpl.list();
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return adminInfoList;

	}

	public AdminInfo login(String account, String password) throws Exception {
		Connection connection = null;
		AdminInfo adminInfo = null;
		try {
			adminInfo = new AdminInfo();
			connection = DataBase.getConn();
			adminInfoDAOImpl.setConn(connection);
			adminInfo = adminInfoDAOImpl.login(account, password);

			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;

		} finally {
			DataBase.releaseConnection(connection);
		}

		return adminInfo;

	}
}
